/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package applety;

import data.Data;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Riso-Desktop
 */
public class hra3 extends javax.swing.JApplet implements Runnable {

    Thread vlakno;
    boolean ide = true;
    boolean nacitane = false;
    int body = 0;
    boolean skontrolovane = false;
    String id;

    /**
     * Initializes the applet hra3
     */
    private void nacitajData(Data data) {
        data.string1 = jLabel1.getText() + jLabel6.getText() + jTextField1.getText() + "=" + jLabel11.getText();
        data.string2 = jLabel2.getText() + jLabel7.getText() + jTextField2.getText() + "=" + jLabel12.getText();
        data.string3 = jLabel3.getText() + jLabel8.getText() + jTextField3.getText() + "=" + jLabel13.getText();
        data.string4 = jLabel4.getText() + jLabel9.getText() + jTextField4.getText() + "=" + jLabel14.getText();
        data.string5 = jLabel5.getText() + jLabel10.getText() + jTextField5.getText() + "=" + jLabel15.getText();
        data.string1 = data.typ + ";" + data.string1 + ";" + data.string2 + ";" + data.string3 + ";" + data.string4 + ";" + data.string5 + ";";
    }

    public void vymysliUlohu() {
        jLabel21.setText("");
        jLabel22.setText("");
        jLabel23.setText("");
        jLabel24.setText("");
        jLabel25.setText("");
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        JLabel[] cisla = {
            jLabel1,
            jLabel2,
            jLabel3,
            jLabel4,
            jLabel5,
            jLabel11,
            jLabel12,
            jLabel13,
            jLabel14,
            jLabel15};
        JLabel[] znaky = {
            jLabel6,
            jLabel7,
            jLabel8,
            jLabel9,
            jLabel10};
        for (int i = 0; i < znaky.length; i++) {
            int n = (int) (2 * Math.random()) + 1;
            if (n == 1) {
                znaky[i].setText("+");
            } else {
                znaky[i].setText("-");
            }
        }
        for (int i = 0; i < cisla.length; i++) {
            int n;
            if (i > 4 && znaky[i - 5].getText().equals("-")) {

                do {
                    n = (int) (21 * Math.random()) + 0;
                } while (n > Integer.parseInt(cisla[i - 5].getText()));
            } else if (i > 4 && znaky[i - 5].getText().equals("+")) {
                do {
                    n = (int) (21 * Math.random()) + 0;
                } while (n < Integer.parseInt(cisla[i - 5].getText()));
            } else {
                n = (int) (21 * Math.random()) + 0;
            }

            cisla[i].setText(n + "");
        }

    }

    @Override
    public void init() {

        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(hra3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(hra3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(hra3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(hra3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        vlakno = new Thread(this);
        this.setSize(260, 300);
        initComponents();
        vymysliUlohu();
        vlakno.start();

    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel26 = new javax.swing.JLabel();

        jLabel5.setText("jLabel5");

        jLabel16.setText("=");

        jLabel3.setText("jLabel3");

        jLabel17.setText("=");

        jLabel4.setText("jLabel4");

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jLabel20.setText("=");

        jLabel18.setText("=");

        jLabel19.setText("=");

        jLabel7.setText("jLabel7");

        jLabel6.setText("jLabel6");

        jLabel23.setText("jLabel23");

        jLabel22.setText("jLabel22");

        jLabel21.setText("jLabel21");

        jLabel25.setText("jLabel25");

        jLabel24.setText("jLabel24");

        jLabel8.setText("jLabel8");

        jLabel9.setText("jLabel9");

        jLabel10.setText("jLabel10");

        jLabel11.setText("jLabel11");

        jLabel12.setText("jLabel12");

        jLabel13.setText("jLabel13");

        jLabel14.setText("jLabel14");

        jLabel15.setText("jLabel15");

        jButton1.setText("Skontrolovať");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Nové úlohy");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel26.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pic/97ea0bfc5f_78490146_o2.gif"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel17)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel12))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel16)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel11)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel19)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel14))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel10)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel20)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel15))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel18)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel13)))
                                .addGap(8, 8, 8)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel21)
                            .addComponent(jLabel22)
                            .addComponent(jLabel23)
                            .addComponent(jLabel24)
                            .addComponent(jLabel25))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel26, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel26, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel16)
                            .addComponent(jLabel21)
                            .addComponent(jLabel11))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel7)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel17)
                            .addComponent(jLabel22)
                            .addComponent(jLabel12))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel8)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel18)
                            .addComponent(jLabel23)
                            .addComponent(jLabel13))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel9)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel19)
                            .addComponent(jLabel24)
                            .addComponent(jLabel14))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jLabel10)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel20)
                            .addComponent(jLabel25)
                            .addComponent(jLabel15))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap(184, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
        if (skontrolovane == true) {
            JOptionPane.showMessageDialog(rootPane, "Tento pokus je už skontrolovaný. Klikni na 'Nové Úlohy'");
            return;
        }
        JLabel[] cisla = {
            jLabel1,
            jLabel2,
            jLabel3,
            jLabel4,
            jLabel5,
            jLabel11,
            jLabel12,
            jLabel13,
            jLabel14,
            jLabel15};
        JLabel[] znaky = {
            jLabel6,
            jLabel7,
            jLabel8,
            jLabel9,
            jLabel10};
        JLabel[] fajky = {
            jLabel21,
            jLabel22,
            jLabel23,
            jLabel24,
            jLabel25};
        JTextField[] odpovede = {
            jTextField1,
            jTextField2,
            jTextField3,
            jTextField4,
            jTextField5};
        try {

            for (int i = 0; i < 5; i++) {
                if (!jTextField1.getText().equals("")) {
                    if (znaky[i].getText().equals("+")) {
                        if (Integer.parseInt(cisla[i + 5].getText()) - Integer.parseInt(odpovede[i].getText()) == Integer.parseInt(cisla[i].getText())) {
                            fajky[i].setText("ok");
                            body++;
                        } else {
                            fajky[i].setText("chyba");
                        }
                    } else {
                        if (Integer.parseInt(cisla[i + 5].getText()) + Integer.parseInt(odpovede[i].getText()) == Integer.parseInt(cisla[i].getText())) {
                            fajky[i].setText("ok");
                            body++;
                        } else {
                            fajky[i].setText("chyba");
                        }
                    }
                }
            }
            skontrolovane = true;
            zapisBody();
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(rootPane, "Všetky polia musia byť vyplnené a obsahovať iba čísla.");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        vymysliUlohu();
        skontrolovane = false;
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    // End of variables declaration//GEN-END:variables

    private void zapisData(String data) {
        JLabel[] cisla = {
            jLabel1,
            jLabel2,
            jLabel3,
            jLabel4,
            jLabel5,
            jLabel11,
            jLabel12,
            jLabel13,
            jLabel14,
            jLabel15};
        JLabel[] znaky = {
            jLabel6,
            jLabel7,
            jLabel8,
            jLabel9,
            jLabel10};
        JTextField[] odpovede = {
            jTextField1,
            jTextField2,
            jTextField3,
            jTextField4,
            jTextField5};
        String d = data;
        if (d.length() != 1 && d.substring(0, 2).equals("3;")) {
            d = d.substring(2);
            for (int i = 0; i < odpovede.length; i++) {
                if (d.indexOf("-") < d.indexOf(";") && d.indexOf("-") != -1) {
                    int znamienko = d.indexOf("-");
                    System.out.println(d);
                    cisla[i].setText(d.substring(0, znamienko));
                    d = d.substring(znamienko + 1);
                    znaky[i].setText("-");
                    odpovede[i].setText(d.substring(0, d.indexOf("=")));
                    d = d.substring(d.indexOf("=") + 1);
                    cisla[i + 5].setText(d.substring(0, d.indexOf(";")));
                    d = d.substring(d.indexOf(";") + 1);
                } else {
                    int znamienko = d.indexOf("+");
                    System.out.println(d);
                    cisla[i].setText(d.substring(0, znamienko));
                    d = d.substring(znamienko + 1);
                    znaky[i].setText("+");
                    odpovede[i].setText(d.substring(0, d.indexOf("=")));
                    d = d.substring(d.indexOf("=") + 1);
                    cisla[i + 5].setText(d.substring(0, d.indexOf(";")));
                    d = d.substring(d.indexOf(";") + 1);
                }
            }
        }
    }
    
    private synchronized void zapisBody() {
        try {

            URL servlet = new URL("http://localhost:8080/Portalpredeti/zapisBody");
            URLConnection con = servlet.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-Type", "application/x-java-serialized-object");

            OutputStream outstream = con.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstream);
            oos.writeObject("3"); //cislo ulohy
            oos.writeObject(id);
            oos.writeObject(body + "/" + "5");
            body = 0;
            oos.flush();
            oos.close();

            InputStream instr = con.getInputStream();
            ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
            String s = (String) inputFromServlet.readObject();
            inputFromServlet.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        try {
            while (ide) {
                Data d = new Data(3);
                if (!skontrolovane) {
                    nacitajData(d);
                }
                URL servlet = new URL("http://localhost:8080/Portalpredeti/zapisDoDB");
                URLConnection con = servlet.openConnection();
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setUseCaches(false);
                con.setRequestProperty("Content-Type", "application/x-java-serialized-object");
                id = getParameter("ID");
                // TODO: toto ID na konci zmazať
                //id = "1";
                if (!nacitane) {
                    OutputStream outstream = con.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(outstream);
                    oos.writeObject("nacitat");

                    oos.writeObject(id);
                    oos.writeObject(d);
                    oos.flush();
                    oos.close();

                    InputStream instr = con.getInputStream();
                    ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
                    String s = (String) inputFromServlet.readObject();
                    if (s != null) {
                        zapisData(s);
                    }
                    inputFromServlet.close();
                    repaint();
                    nacitane = true;
                } else if(!skontrolovane){
                    OutputStream outstream = con.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(outstream);
                    oos.writeObject("zapis");

                    oos.writeObject(id);
                    oos.writeObject(d);
                    oos.flush();
                    oos.close();
                    InputStream instr = con.getInputStream();
                    ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
                    String c = (String) inputFromServlet.readObject();
                    System.out.println(c);
                    inputFromServlet.close();

                }
                vlakno.sleep(10000);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}
